package com.boe.admin.controller;


import com.boe.admin.entity.Annex;
import com.boe.admin.model.request.AnnexRequest;
import com.boe.admin.model.vo.AnnexUpdateVO;
import com.boe.admin.model.vo.AnnexVO;
import com.boe.admin.service.IAnnexService;
import com.boe.common.annotation.OpenAction;
import com.boe.common.mvc.ApiResult;
import com.boe.common.mvc.ListPageRequest;
import com.boe.common.mvc.ListPageResult;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Yuxh
 * @since 2022-11-02
 */
@RestController
@RequestMapping("/admin/annex")
@RequiredArgsConstructor
@OpenAction
public class AnnexController {

    @NotNull
    private IAnnexService annexService;


    @PostMapping("/list")
    public ListPageResult<AnnexVO> list(@RequestBody ListPageRequest<AnnexRequest> requestParam) {
        var ret = annexService.list(requestParam.getQueryBody(), requestParam.getPageNo(), requestParam.getPageSize(), requestParam.getOrder());

        return ret;
    }

    @PostMapping("/detail/{id}")
    public ApiResult<AnnexVO> detail(@PathVariable String id) {
        var ret = annexService.getById(id);
        //BeanUtils.copyProperties
        return ApiResult.of(ret);
    }

    @PostMapping("/add")
    public ApiResult<Annex> add(@RequestBody Annex annex) {
        if (annex.getId() != null && !annex.getId().isEmpty()) {
            return ApiResult.error("添加时, id 必须是空!");
        }

        if (annexService.save(annex) == false) {
            return ApiResult.error("添加失败!");
        }

        return ApiResult.of(annex);
    }


    @PostMapping("/update/{id}")
    public ApiResult<String> update(@PathVariable String id, @RequestBody AnnexUpdateVO annexVO) {

        if (annexVO.getId() != null && !annexVO.getId().isEmpty()) {
            return ApiResult.error("添加时, id 必须是空!");
        }


        if (annexService.update(id, annexVO) == false) {
            return ApiResult.error("更新失败!");
        }

        return new ApiResult();
    }


    @PostMapping("/delete/{id}")
    public ApiResult<String> delete(@PathVariable String id) {

        if (annexService.removeById(id) == false) {
            return ApiResult.error("删除失败!");
        }

        return new ApiResult();
    }
}
